package zhongbo.java.air.dictionary.service.ServiceImpl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.BusinessException;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.dictionary.mapper.AirPortMapper;
import zhongbo.java.air.dictionary.model.AirPort;
import zhongbo.java.air.dictionary.service.IAirPortService;

import java.util.Date;

@Service
public class IAirPortServiceImpl extends ServiceImpl<AirPortMapper, AirPort> implements IAirPortService {

    @Autowired
    private AirPortMapper airPortMapper;

    @Override
    public PageVo<AirPort> pageQuery(Integer page, Integer pageSize
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<AirPort> queryWrapper = new QueryWrapper<AirPort>();
        Page<AirPort> airPortPage = new Page<>(page,pageSize);

        if (oderByField == null || oderByField.isEmpty()){
            queryWrapper.orderByAsc("update_time");
        }else {
            if (orderByAsc){
                queryWrapper.orderByAsc(oderByField);
            }else {
                queryWrapper.orderByDesc(oderByField);
            }
        }

        //指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)){
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        Page<AirPort> airCraftPage1 = airPortMapper.selectPage(airPortPage, queryWrapper);
        return new PageVo<>(airCraftPage1);
    }

    @Override
    public ResultVo saveAirPort(AirPort airPort) {
        airPort.setCreateTime(new Date());
        airPort.setUpdateTime(new Date());

        if (airPort.getIcaoCode() == null || airPort.getIcaoCode().isEmpty()){
            throw new BusinessException("机场代码不能为空");
        }else if (airPort.getIcaoCode().length() > 4){
            throw new BusinessException("机场代码长度不能大于4");
        }

        if (airPort.getIataCode() == null || airPort.getIataCode().isEmpty()){
            throw new BusinessException("IATA代码不能为空");
        }else if (airPort.getIataCode().length() > 3){
            throw new BusinessException("IATA代码不能大于3");
        }

        if (airPort.getName() == null || airPort.getName().isEmpty()){
            throw new BusinessException("机场名称不能为空");
        }

        if (airPort.getLatitude() == null || airPort.getLatitude().toString().isEmpty()){
            throw new BusinessException("纬度不能为空");
        }

        if (airPort.getLongitude() == null || airPort.getLongitude().toString().isEmpty()){
            throw new BusinessException("经度不能为空");
        }

        if (airPort.getStatus() == null || airPort.getStatus().isEmpty()){
            throw new BusinessException("状态不能为空");
        }

        if (airPort.getTimezone() == null || airPort.getTimezone().isEmpty()){
            throw new BusinessException("时区不能为空");
        }

        LambdaQueryWrapper<AirPort> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AirPort::getIcaoCode,airPort.getIcaoCode());
        if (!airPortMapper.selectList(queryWrapper).isEmpty()){
            throw new BusinessException("机场代码只能是唯一编号");
        }
        queryWrapper.clear();
        queryWrapper.eq(AirPort::getIataCode,airPort.getIataCode());
        if (!airPortMapper.selectList(queryWrapper).isEmpty()){
            throw new BusinessException("IATA代码只能是唯一编号");
        }

        if (!(airPortMapper.insert(airPort) > 0)){
            return ResultVo.error("添加失败");
        }

        return ResultVo.ok("添加成功");
    }

    @Override
    public ResultVo updateAirPort(AirPort airPort) {
        airPort.setUpdateTime(new Date());
        if (airPort.getId() == null || airPort.getId().isEmpty()){
            return ResultVo.error("id不能为空");
        }

        LambdaQueryWrapper<AirPort> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AirPort::getIcaoCode,airPort.getIcaoCode());
        if (!airPortMapper.selectList(queryWrapper).isEmpty()){
            throw new BusinessException("机场代码只能是唯一编号");
        }
        queryWrapper.clear();
        queryWrapper.eq(AirPort::getIataCode,airPort.getIataCode());
        if (!airPortMapper.selectList(queryWrapper).isEmpty()){
            throw new BusinessException("IATA代码只能是唯一编号");
        }


        if (!(airPortMapper.updateById(airPort) > 0)){
            return ResultVo.error("修改失败,不存在该机场");
        }
        return ResultVo.ok("修改成功!");
    }
}
